package org.spring.springboot.server.Impl;

import org.spring.springboot.dao.SystemManageDao;
import org.spring.springboot.exception.APIException;
import org.spring.springboot.exception.CommonBizException;
import org.spring.springboot.exception.ExpCodeEnum;
import org.spring.springboot.po.SysUserExample;
import org.spring.springboot.po.SystemManagePO;
import org.spring.springboot.server.SystemManageService;
import org.spring.springboot.util.ConstantUtil;
import org.spring.springboot.util.MD5Util;
import org.spring.springboot.vo.SystemManageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class SystemManageServiceImpl implements SystemManageService {
    @Autowired
    private SystemManageDao systemManageDao;

    @Override
    public void deleteSystemUser(Integer id) {
        systemManageDao.delete(id);
    }

    @Override
    public List<Map<String, Object>> getSystemNameList() {
        return systemManageDao.getSystemNameList();
    }

    @Override
    public void updateUserId(Integer userid ,Integer id) {
        systemManageDao.disable(userid, id);
    }

    @Override
    public List<Map<String, Object>> getSystemUserList() {
        return systemManageDao.selectSystemUserList();
    }

    @Override
    public void addSystemUser(SystemManageVO systemManageVO) {
        //用户名称不能重复
        SysUserExample sysUserExample = new SysUserExample();
        sysUserExample.createCriteria().andUserNameEqualTo(systemManageVO.getUsername());
        List<SystemManagePO> sysUsers = systemManageDao.selectByExample(sysUserExample);
        if(sysUsers.size() > ConstantUtil.ZERO) {
            throw new APIException("管理员用户名重复");
        }else {
            //insert
            SystemManagePO systemManagePO = new SystemManagePO();
            BeanUtils.copyProperties(systemManageVO,systemManagePO);
            String fpassword = systemManageVO.getPassword() + ConstantUtil.salt;
            fpassword = MD5Util.getMd5(fpassword);
            systemManagePO.setPassword(fpassword);
            systemManageDao.insert(systemManagePO);
        }
    }

    @Override
    //修改密码
    public void updatePassword(SystemManageVO systemManageVO) {
        SystemManagePO systemManagePO = new SystemManagePO();
        BeanUtils.copyProperties(systemManageVO,systemManagePO);
        String fpassword = systemManageVO.getPassword() + ConstantUtil.salt;
        fpassword = MD5Util.getMd5(fpassword);
        systemManagePO.setPassword(fpassword);
        int result = systemManageDao.updateByPrimaryKeySelective(systemManagePO);
        if(result == ConstantUtil.ZERO) {
            throw new APIException("管理员信息更新失败");
        }
    }

    @Override
    public SystemManageVO sysLogin(String username, String password) {
        //判断账号密码是否正确
        String fpassword = password + ConstantUtil.salt;
        fpassword = MD5Util.getMd5(fpassword);
        SysUserExample sysUserExample = new SysUserExample();
        sysUserExample.createCriteria().andUserNameEqualTo(username).andPasswordEqualTo(fpassword);
        List<SystemManagePO> sysUsers = systemManageDao.selectByExample(sysUserExample);
        if(sysUsers.size() > ConstantUtil.ZERO) {
            SystemManageVO systemManageVO = new SystemManageVO();
            BeanUtils.copyProperties(sysUsers.get(0), systemManageVO);
            return systemManageVO;
        }else {
            throw new APIException("登录失败");
        }
    }


}
